package club.ljwang.controller;

import club.ljwang.constant.MessageConstant;
import club.ljwang.entity.PageResult;
import club.ljwang.entity.QueryPageBean;
import club.ljwang.entity.Result;
import club.ljwang.exception.CheckItemException;
import club.ljwang.exception.PermissionException;
import club.ljwang.pojo.CheckItem;
import club.ljwang.pojo.Permission;
import club.ljwang.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: wangl
 * @Date: 2020/3/9 11:38
 * @Description:
 */

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return permissionService.findPage(queryPageBean);
    }


    @RequestMapping("add")
    public Result add(@RequestBody Permission permission) {

        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }

    }



    @RequestMapping("delete")
    public Result add(Integer id) {
        try {
            permissionService.delete(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (PermissionException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL + "," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }

    }

    @RequestMapping("findById")
    public Result findById(Integer id) {
        Permission permission = permissionService.findById(id);

        if (permission == null) {

        }
        return new Result(true, "", permission);
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody Permission permission) {

        try {
            permissionService.edit(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }

    }
}
